/**
 * Copyright 2014 lerr
 * 
 * All right reserved.
 * 
 * Dec 8, 2014
 */
package com.bac.bacplatform;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.bac.http.VolleyErrorHelper;
import com.bac.javabeans.Principal;
import com.bac.utils.Config;
import com.bac.utils.LockPatternUtils;
import com.bac.utils.Storage;
import com.bac.utils.Util;
import com.google.gson.Gson;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author lerry
 * 
 */
public class FragmentPersonal extends BaseFragment implements OnClickListener{
	
	private PopupWindow mPopupWindow;
	
	private TextView mTVaccount, mTVEmail;
	
	private LinearLayout mLogoutLay;
	
	private boolean mIsLogin = false;

	private LockPatternUtils lockPatternUtils;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.personal_center, null);
		initView(view);
		return view;
	}

	@SuppressLint("CutPasteId")
	public void initView(View view) {
		RelativeLayout personalProfile = (RelativeLayout)view.findViewById(R.id.ly_account);
		personalProfile.setOnClickListener(this);

		RelativeLayout personalWallet = (RelativeLayout)view.findViewById(R.id.ly_my_wallet);
		personalWallet.setOnClickListener(this);
		
		RelativeLayout personalCoupon = (RelativeLayout)view.findViewById(R.id.ly_my_coupon);
		personalCoupon.setOnClickListener(this);
		
		RelativeLayout personalBills= (RelativeLayout)view.findViewById(R.id.ly_my_bills);
		personalBills.setOnClickListener(this);
		
		RelativeLayout personalPwd = (RelativeLayout)view.findViewById(R.id.ly_my_pwd);
		personalPwd.setOnClickListener(this);
		
		LinearLayout logout = (LinearLayout)view.findViewById(R.id.ly_logout);
		logout.setOnClickListener(this);
		
		View include = (View)view.findViewById(R.id.title_bar);
		include.findViewById(R.id.lay_more).setOnClickListener(this);
		
		mTVaccount = (TextView)view.findViewById(R.id.tv_userid);
		mTVEmail = (TextView)view.findViewById(R.id.tv_user_email);
		mLogoutLay = (LinearLayout)view.findViewById(R.id.ly_logout);
		
		lockPatternUtils = new LockPatternUtils(getActivity());
	}

	
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		Log.i("FragmentPersonal----", "hidden: "+hidden);
		if (!hidden) {
			getCurrentuser();
		}
		
	}

	@Override
	public void onClick(View v) {
		
		if (!mIsLogin) {
			((MainActivity)getActivity()).hanlderAutorError();
			return;
		}
		switch (v.getId()) {
		case R.id.ly_account:
			Intent intentToPersonal = new Intent(getActivity(),
					PersonalProfileActivity.class);
			startActivityIn(intentToPersonal);
			break;
		case R.id.ly_my_wallet:
//			Intent intentToMyWallet = new Intent(getActivity(),MyWalletActivity.class);
//			startActivityIn(intentToMyWallet);
			if (Config.UserGeneralInfo.wealthAccountBanding) {
				Intent intentToMyWallet = new Intent(getActivity(), MyWalletActivity.class);
				startActivityIn(intentToMyWallet);
			}else{
				Intent intentToFindPink = new Intent(getActivity(), FindPickActivity.class);
				startActivityIn(intentToFindPink);
			}
			break;
		case R.id.ly_my_coupon:
			Intent intentToCoupon = new Intent(getActivity(),
					PersonalCouponActivity.class);
			startActivityIn(intentToCoupon);
			break;
		case R.id.ly_my_bills:
			Intent intentToBills = new Intent(getActivity(),BillActivity.class);
			startActivityIn(intentToBills);
			break;
		case R.id.ly_my_pwd:
			Intent intentToMypwd = new Intent(getActivity(),
					PersonalPwdManagerActivity.class);
			startActivityIn(intentToMypwd);
			break;
		case R.id.ly_logout:
			logout();
			break;
		case R.id.lay_more:
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
 				return;
			} else {
				initmPopupWindowView();
				mPopupWindow.showAsDropDown(v, -70, 0);
				
			}
			break;
		default:
			break;
		}
	}
	
	private void initmPopupWindowView() {
		View customView = getLayoutInflater(getArguments()).inflate(R.layout.more, null);
		mPopupWindow = new PopupWindow(customView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		customView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (mPopupWindow != null && mPopupWindow.isShowing()) {
					mPopupWindow.dismiss();
					mPopupWindow = null;
				}
				return false;
			}
		});
		//拨打客服电话
		RelativeLayout call_us = (RelativeLayout) customView.findViewById(R.id.call_us);
		call_us.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//拨打电话
				String number = "4001106262";
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+number));
				startActivity(intent);
				mPopupWindow.dismiss();
			}
		});
		//查看关于我们
		RelativeLayout about_us = (RelativeLayout) customView.findViewById(R.id.about_us);
		about_us.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 查看关于我们协议
				Intent intentToAgree = new Intent(getActivity(),
						BacAgreementWebActivity.class);
				intentToAgree.putExtra("urltype", "about_us");
				startActivityIn(intentToAgree);
				mPopupWindow.dismiss();
			}
		});
	}
	
	public void logout(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgressNow();
				Log.i("TAG----success", response.toString());
				Storage.deleteStringForKey(getActivity(), Config.USER_PWD_KEY);
				Log.i(getClass().getName(), "pwd2-------"+Storage.getString(getActivity(), Config.USER_PWD_KEY));
				mIsLogin = false;
				mTVaccount.setText("");
				mTVEmail.setText(R.string.personal_user_email_test);
				mLogoutLay.setVisibility(View.GONE);
				lockPatternUtils.clearLock();
				((MainActivity)getActivity()).goHome();
//				Intent intentToLogin = new Intent(getActivity(), LoginActivity.class);
//				startActivityIn(intentToLogin);
//				finishActivityOut();
			}
		};

		showLoadingProgress();
		Log.i(getClass().getName(), "pwd1-------"+Storage.getString(getActivity(), Config.USER_PWD_KEY));
		String path = Config.URL_LOGOUT;
		Util.httpRequestToGet(path, sL, this);
		
	}
	
	public void getCurrentuser() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Util.dismissProgressDialog();
				mIsLogin = true;
				Log.i("FragmentPersonal----success", response.toString());
				Gson gson = new Gson();
				Principal principal = gson.fromJson(response, Principal.class);
				Config.UserGeneralInfo.setUserInfo(principal);
				if(!"".equals(principal.getPhone())){
					mTVaccount.setText(principal.getPhone());
				}
				if (principal.getEmail() != null && !"".equals(principal.getEmail())) {
					mTVEmail.setText(principal.getEmail());
				}
				
				mLogoutLay.setVisibility(View.VISIBLE);
			}
		};
		
		showLoadingProgress();
		String path = Config.URL_CURRENT_USER;
		Util.httpRequestToGet(path, sL, this);
	}
	
}
